package com.zqwh.yj.sdk.model;

import android.app.smdt.SmdtManager;
import android.content.Context;

public class LockModelImpl implements LockModel {
    public static final int GPIO_DIR_OUT = 1;
    public static final int GPIO_DIR_IN = 0;

    public static final int GPIO_LEVEL_H = 1;
    public static final int GPIO_LEVEL_L = 0;

    public static final int GPIO_LOCK_1_CONTROL = 2;
    public static final int GPIO_LOCK_1_DETECT_DOOR = 3;
    public static final int GPIO_LOCK_1_DETECT_BOLT = 1;

    public static final int GPIO_LOCK_2_CONTROL = 6;
    public static final int GPIO_LOCK_2_DETECT_DOOR = 7;
    public static final int GPIO_LOCK_2_DETECT_BOLT = 5;

    private Context mContext;
    private SmdtManager mSmdtManager;

    public LockModelImpl(Context context) {
        mContext = context;
        mSmdtManager = new SmdtManager(mContext);

        mSmdtManager.smdtSetXrm117xGpioDirection(GPIO_LOCK_1_CONTROL, GPIO_DIR_OUT, GPIO_LEVEL_H);
        mSmdtManager.smdtSetXrm117xGpioDirection(GPIO_LOCK_1_DETECT_DOOR, GPIO_DIR_IN, GPIO_LEVEL_H);
        mSmdtManager.smdtSetXrm117xGpioDirection(GPIO_LOCK_1_DETECT_BOLT, GPIO_DIR_IN, GPIO_LEVEL_H);

        mSmdtManager.smdtSetXrm117xGpioDirection(GPIO_LOCK_2_CONTROL, GPIO_DIR_OUT, GPIO_LEVEL_H);
        mSmdtManager.smdtSetXrm117xGpioDirection(GPIO_LOCK_2_DETECT_DOOR, GPIO_DIR_IN, GPIO_LEVEL_H);
        mSmdtManager.smdtSetXrm117xGpioDirection(GPIO_LOCK_2_DETECT_BOLT, GPIO_DIR_IN, GPIO_LEVEL_H);
    }

    @Override
    public void unlock1() {
        mSmdtManager.smdtSetXrm117xGpioValue(GPIO_LOCK_1_CONTROL, GPIO_LEVEL_L);
    }

    @Override
    public void lock1() {
        mSmdtManager.smdtSetXrm117xGpioValue(GPIO_LOCK_1_CONTROL, GPIO_LEVEL_H);
    }

    @Override
    public int[] getLock1Status() {
        int doorStatus = mSmdtManager.smdtGetXrm117xGpioValue(GPIO_LOCK_1_DETECT_DOOR);
        int boltStatus = mSmdtManager.smdtGetXrm117xGpioValue(GPIO_LOCK_1_DETECT_BOLT);
        return new int[]{doorStatus, boltStatus};
    }

    @Override
    public void unlock2() {
        mSmdtManager.smdtSetXrm117xGpioValue(GPIO_LOCK_2_CONTROL, GPIO_LEVEL_L);
    }

    @Override
    public void lock2() {
        mSmdtManager.smdtSetXrm117xGpioValue(GPIO_LOCK_2_CONTROL, GPIO_LEVEL_H);
    }

    @Override
    public int[] getLock2Status() {
        int doorStatus = mSmdtManager.smdtGetXrm117xGpioValue(GPIO_LOCK_2_DETECT_DOOR);
        int boltStatus = mSmdtManager.smdtGetXrm117xGpioValue(GPIO_LOCK_2_DETECT_BOLT);
        return new int[]{doorStatus, boltStatus};
    }
}
